<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/5/30
 * Time: 14:36
 */
namespace app\admin\controller;

use app\common\Common;
use app\data\ShopData;
use app\data\UserData;
use think\Db;
use think\Cache;
class Audit extends Base
{
//    //店铺审核管理
//    public function management($data,$page=1,$pre=15,$type=1){
//        $id = cache($data);
//        if(!$id)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
//        $b = ($page-1)*$pre;
//        switch ($type){
//            case 0:
//                $data = Common::db_count("id,shop_name","","drh_shop","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            case 1:
//                $data = Common::db_count("id,shop_name","","drh_shop","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            case 2;
//                $data = Common::db_count("id,shop_name","","drh_shop","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            default;
//                $data ="参数错误";
//        }
//        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>$data]);
//
//    }
//    //店铺审核修改
//    public function alter($data,$string,$id,$mun=1){
//        $ids = cache($data);
//        if(!empty($ids)){
//            return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
//        }
//        switch ($mun) {
//            case 1:
//                Common::db_upload("drh_shop","verify_status='".$mun."',reason='".$string."' WHERE id=".$id);
//                return json(['code'=>$this->t_code,'msg'=>"ok"]);
//                break;
//            case 2;
//                Common::db_upload("drh_shop","verify_status='".$mun."',reason='".$string."' WHERE id=".$id);
//                return json(['code'=>$this->t_code,'msg'=>"ok"]);
//                break;
//            default:
//                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
//        }
//    }
    //提现审核查看
    public function withdraw($data,$page=1,$pre=15,$type=1){
        $id = cache($data);
        if(!$id)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        $b = ($page-1)*$pre;
        switch ($type){
            case 0;
                $data = Common::db_count("id,total_money","","drh_withdraw","review_status=".$type." LIMIT ".$b.",".$pre);
                break;
            case 1:
                $data = Common::db_count("id,total_money","","drh_withdraw","review_status=".$type." LIMIT ".$b.",".$pre);
                break;
            case 2;
                $data = Common::db_count("id,shop_name","","drh_withdraw","review_status=".$type." LIMIT ".$b.",".$pre);
                break;
            default;
                $data ="参数错误";
        }
        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>$data]);
    }
    //提现审核
    public function withdraw_alter($data,$string,$id,$mun=1){
        $ids = cache($data);
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        switch ($mun) {
            case 1:
                Common::db_upload("drh_withdraw","review_status='".$mun."',msg='".$string."' WHERE id=".$id);
                return json(['code'=>$this->t_code,'msg'=>"ok"]);
                break;
            case 2;
                Common::db_upload("drh_withdraw","review_status='".$mun."',msg='".$string."' WHERE id=".$id);
                return json(['code'=>$this->t_code,'msg'=>"ok"]);
                break;
            default:
                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
        }
    }
//    //创始会员审核查看
//    public function originate($data,$page=1,$pre=15,$type=1){
//        $id = cache($data);
//        if(!$id)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
//        $b = ($page-1)*$pre;
//        switch ($type){
//            case 0;
//                $data = Common::db_count("id,user_name","","drh_original_member","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            case 1:
//                $data = Common::db_count("id,user_name","","drh_original_member","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            case 2;
//                $data = Common::db_count("id,user_name","","drh_original_member","verify_status=".$type." LIMIT ".$b.",".$pre);
//                break;
//            default;
//                $data ="参数错误";
//        }
//        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>$data]);
//    }
//    //创始会员
//    public function originate_alter($data,$string,$id,$mun=1){
//        $ids = cache($data);
//        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
//        switch ($mun) {
//            case 1:
//                Common::db_upload("drh_original_member","verify_status='".$mun."',msg='".$string."' WHERE id=".$id);
//                return json(['code'=>$this->t_code,'msg'=>"ok"]);
//                break;
//            case 2;
//                Common::db_upload("drh_original_member","verify_status='".$mun."',msg='".$string."' WHERE id=".$id);
//                return json(['code'=>$this->t_code,'msg'=>"ok"]);
//                break;
//            default:
//                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
//        }
//    }
    //积分质押审核查看
    public function integral($data,$page=1,$pre=15,$type=1){
        $id = cache($data);
        if(!$id)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        $b = ($page-1)*$pre;
        switch ($type){
            case 0;
                $data = Common::db_count("*","","drh_integral_withdraw","verify_status=".$type." LIMIT ".$b.",".$pre);
                break;
            case 1:
                $data = Common::db_count("*","","drh_integral_withdraw","verify_status !=0  LIMIT ".$b.",".$pre);
                break;
            default;
                $data ="参数错误";
        }
        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>$data]);
    }
    //积分质押
    public function integral_alter($data,$string,$id,$mun=1){
        $ids = cache($data);
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        switch ($mun) {
            case 1:
                Common::db_upload("drh_integral_withdraw","verify_status='".$mun."',msg='".$string."' WHERE id=".$id);
                return json(['code'=>$this->t_code,'msg'=>"ok"]);
                break;
            case 2;
                Common::db_upload("drh_integral_withdraw","verify_status='".$mun."',msg='".$string."' WHERE id=".$id);
                return json(['code'=>$this->t_code,'msg'=>"ok"]);
                break;
            default:
                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
        }
    }
    //会员商户交子回收查看
    public function recycling($data,$page=0,$pre=10,$type=0){
        $b = ($page-1)*$pre;
        $ids = cache($data);
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        $recycling = Common::db_count('a.id,a.user_id,a.audit as verify_status,a.money,a.apply_for_time,b.user_name','',"drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id ","audit={$type} and is_not=0 limit {$b},{$pre}");
        foreach ($recycling as $k=>$v){
            $recycling[$k]['time'] = date('Y-m-d H:i:s',$v['apply_for_time']);
            unset($recycling[$k]['apply_for_time']);
        }
        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>['list'=>$recycling,'total'=> Common::db_count_id('count(a.id)','id',"drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id ","audit={$type} and is_not=0")]]);
    }
    //会员商户交子回收审核
    public function recycling_alter($data,$id,$mag='',$annotation='',$mun=1){
        $ids = cache($data);
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        switch ($mun) {
            case 1:
                Db::startTrans();
                try{
                    $water = Common::db_count('a.id,a.money,b.user_integral,b.user_open_id','','drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id',"a.id={$id} and is_not=0")[0]??array();
                    if($water == array())  return json(['code'=>$this->f_code,'msg'=>"系统繁忙"]);
                    $integral = bcsub($water['user_integral'],$water['money'],0);
                    Common::db_upload('drh_user_info',"user_integral={$integral} WHERE id={$water['id']}");
                    $copulation = unserialize(Common::db_count('copulation',"",'drh_parameter')[0]['copulation'])??array();
                    //用户提现零钱金额
                    $money = bcmul($water['money'],$copulation['sum'],0)*100;
                    if($water['money'] >=34 && $money >=30){
                        Common::db_upload("drh_recycling_water","audit='".$mun."',msg='".$mag."',annotation='".$annotation."',audit_time='".time()."' WHERE id=".$id);
                        Common::db_insert('drh_msg',['user_id'=>$ids,'type_status'=>0,'msg'=>'审核成功','create_time'=>time(),'status'=>1]);
                        $res =  $this->txFunc($water['user_open_id'],$money,'交子回收提现');
                        if(!empty($res['err_code'])){
                            return json(['code'=>$this->f_code,'msg'=>"公司账户余额不足,微信提示:".$res['err_code_des']]);
                        }
                        //
                        Db::commit();
                        return json(['code'=>$this->t_code,'msg'=>"审核成功"]);
                    }else{
                        return json(['code'=>$this->f_code,'msg'=>"单次提现不低于0.3"]);
                    }

                }catch (\Exception $e){
                    Db::rollback();
                    return json(['code'=>$this->f_code,'msg'=>"系统繁忙"]);
                }
                break;
            default:
                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
        }
    }
    //会员商户余额审核查看
    public function cash($data,$page=0,$pre=10,$type=0){
        $ids = cache($data);
        $b = ($page-1)*$pre;
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        $recycling = Common::db_count('a.id,a.user_id,a.audit as verify_status,a.money,a.apply_for_time,b.user_name','',"drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id ","audit={$type} and is_not=1 limit {$b},{$pre}");
        foreach ($recycling as $k=>$v){
            $recycling[$k]['time'] = date('Y-m-d H:i:s',$v['apply_for_time']);
            unset($recycling[$k]['apply_for_time']);
        }
        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>['list'=>$recycling,'total'=> Common::db_count_id('count(a.id)','id',"drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id ","audit={$type} and is_not=1")]]);
    }
    //会员商户余额审核
    public function cash_alter($data,$id,$mag='',$annotation='',$mun=1){
        $ids = cache($data);
        if(!$ids)return json(['code'=>$this->f_code,'msg'=>"登录状态错误"]);
        switch ($mun) {
            case 1:
                Db::startTrans();
                try{
                    $water = Common::db_count('a.id,a.money,b.user_money,b.user_open_id','','drh_recycling_water as a left join drh_user_info as b on a.user_id=b.id',"a.id={$id} and is_not=1 and audit=0")[0]??array();
                    if($water == array())  return json(['code'=>$this->f_code,'msg'=>"系统繁忙"]);

                    $integral = bcsub($water['user_money'],$water['money'],0);
                    Common::db_upload('drh_user_info',"user_money={$integral} WHERE id={$water['id']}");
                    $withdraw_deposit = unserialize(Common::db_count('withdraw_deposit',"",'drh_parameter')[0]['withdraw_deposit'])??array();
                    //用户提现零钱金额
                    $money =bcsub($water['money'],bcmul($water['money'],bcdiv($withdraw_deposit['service_charge'],10,2),2));
                    if($water['money'] >=34 && $money >=30){
                        Common::db_upload("drh_recycling_water","audit='".$mun."',msg='".$mag."',annotation='".$annotation."',audit_time='".time()."' WHERE id=".$id);
                        Common::db_insert('drh_msg',['user_id'=>$ids,'type_status'=>0,'msg'=>'审核成功','create_time'=>time(),'status'=>1]);
                        $res = $this->txFunc(/*$water['user_open_id']*/'ot8G05C9EExkYVevXjm_A0RAi5Ug',$money,'商户余额提现');
                        if(!empty($res['err_code'])){
                            return json(['code'=>$this->f_code,'msg'=>"公司账户余额不足,微信提示:".$res['err_code_des']]);
                        }
                        Db::commit();
                        return json(['code'=>$this->t_code,'msg'=>"审核成功"]);
                    }else{
                        return json(['code'=>$this->f_code,'msg'=>"单次提现不低于0.3"]);
                    }
                }catch (\Exception $e){
                    Db::rollback();
                    return json(['code'=>$this->f_code,'msg'=>$e->getMessage()]);
                }
                break;
            default:
                return json(['code'=>$this->f_code,'msg'=>"传递了错误的参数"]);
        }
    }
    public function supplement($user_id,$start=1){
        if (!Common::checkNumber($user_id)) {
            return json(['code' => $this->t_code, 'msg' => '非法操作']);
        }
        Db::startTrans();
        try{
            Common::db_upload('drh_original_member',"identity={$start}");
            Common::db_insert('drh_msg',['user_id'=>$user_id,'type_status'=>0,'msg'=>'审核成功','create_time'=>time(),'status'=>1]);
            Db::commit();
            return json(['code'=>$this->t_code,'msg'=>"审核成功"]);
        }catch (\Exception $e){
            Db::rollback();
            return json(['code'=>$this->f_code,'msg'=>"系统繁忙"]);
        }
    }
}